package com.work.saturn.interesting;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.immo.libcommon.base.BaseHeadActivity;
import com.immo.libcommon.constant.BaseUrl;
import com.immo.libcommon.http.NetworkRequest;
import com.immo.libcommon.utils.FullyLinearLayoutManager;
import com.work.saturn.R;
import com.work.saturn.bean.InterestDetailBean;
import com.work.saturn.bean.InterestSubmitBean;
import com.work.saturn.bean.MessageEvent;
import com.immo.libcommon.constant.RouterUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author YaoChen
 * @content 未来局详情
 * @date 2018/07/16
 */
@Route(path = RouterUtil.INTER_DETAIL)
public class InterestDetailActivity extends BaseHeadActivity {
    @BindView(R.id.interest_detail_title)
    TextView interestDetailTitle;
    @BindView(R.id.interest_detail_person)
    TextView interestDetailPerson;
    @BindView(R.id.interest_detail_time)
    TextView interestDetailTime;
    @BindView(R.id.interest_detail_list)
    RecyclerView mList;
    @BindView(R.id.interest_detail_password)
    EditText interestDetailPassword;
    @BindView(R.id.interest_detail_ll)
    LinearLayout interestDetailLl;
    @BindView(R.id.interest_detail_back)
    TextView interestDetailBack;
    @BindView(R.id.interest_detail_submit)
    TextView interestDetailDW;
    private List<String> datas = new ArrayList<>();
    private String interName = "worldCup";
    private int selectId = -1;
    private InterestDetailBean interestDetailBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interset_detail);
        //禁止进入时弹出软键盘
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        ButterKnife.bind(this);
        setTitle("未来局");
        interName = getIntent().getStringExtra("name");
        setTitleRight("下注统计", 0);
        interestDetailNet();

    }

    @Override
    protected void onRightClick() {
        super.onRightClick();
        ARouter.getInstance().build(RouterUtil.INTER_HISTORY)
                .withSerializable("interesthistory", (Serializable) interestDetailBean.getPayload().getPayload().getHistory())
                .navigation();
    }

    /**
     * 未来局详情接口请求
     */
    @SuppressLint("SetTextI18n")
    private void interestDetailNet() {
        HashMap<String, String> params = new HashMap<>();
        params.put("futureBureauName", interName);
        new NetworkRequest((object, data) -> {
            if (object instanceof InterestDetailBean) {
                interestDetailBean = (InterestDetailBean) object;
                if (interestDetailBean.isSuccess()) {
                    interestDetailTitle.setText(interestDetailBean.getPayload().getPayload().getContent());
                    interestDetailPerson.setText("公证人：" + interestDetailBean.getPayload().getPayload().getJudgePerson());
                    interestDetailTime.setText(interestDetailBean.getPayload().getPayload().getEndTime() + "结束");
                    interestDetailDW.setText("GZH");
                    datas.add(interestDetailBean.getPayload().getPayload().getOption1());
                    datas.add(interestDetailBean.getPayload().getPayload().getOption2());
                    datas.add(interestDetailBean.getPayload().getPayload().getOption3());
                    datas.add(interestDetailBean.getPayload().getPayload().getOption4());
                    datas.add(interestDetailBean.getPayload().getPayload().getOption5());
                    initData();
                }
            }
        }).requestPost(BaseUrl.INTERESTDETAIL, this, JSON.toJSONString(params), InterestDetailBean.class, true, false);
    }

    /**
     * 未来局下注接口请求
     */
    private void interestSubmitNet() {
        HashMap<String, String> params = new HashMap<>();
        params.put("target", interestDetailBean.getPayload().getPayload().getCreator());
        params.put("futureBureauName", interestDetailBean.getPayload().getPayload().getName());
        params.put("chooseOption", datas.get(selectId));
        params.put("tokenName", "GZH");
        params.put("amount", interestDetailPassword.getText().toString());

        new NetworkRequest((object, data) -> {
            if (object instanceof InterestSubmitBean) {
                InterestSubmitBean interestSubmitBean = (InterestSubmitBean) object;
                if (interestSubmitBean.isSuccess()) {
                    toast("提交成功");
                    EventBus.getDefault().post(new MessageEvent("更新首页数据"));
                    finish();
                }
            }
        }).requestPost(BaseUrl.INTERESTSUBMIT, this, JSON.toJSONString(params), InterestSubmitBean.class, true, false);
    }

    private void initData() {
        FullyLinearLayoutManager linearLayoutManagerTheme = new FullyLinearLayoutManager(this);
        linearLayoutManagerTheme.setOrientation(LinearLayoutManager.VERTICAL);
        mList.setLayoutManager(linearLayoutManagerTheme);
        InterestingListAdapter releaseDetailAdapter = new InterestingListAdapter();
        //设置适配器
        releaseDetailAdapter.bindToRecyclerView(mList);
    }

    /**
     * 资产历史列表
     */
    private class InterestingListAdapter extends BaseQuickAdapter<String, BaseViewHolder> implements BaseQuickAdapter.OnItemClickListener {
        InterestingListAdapter() {
            super(R.layout.item_interes_detail, datas);
            setOnItemClickListener(this);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.interest_detail_person, item);
            if (helper.getLayoutPosition() == selectId) {
                helper.setVisible(R.id.interest_detail_img, true);
            } else {
                helper.setVisible(R.id.interest_detail_img, false);
            }
        }

        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            selectId = position;
            notifyDataSetChanged();
        }
    }

    /**
     * 输入的本地判断
     *
     * @return true 通过 false 不通过
     */
    private boolean isCheck() {
        if (selectId == -1) {
            toast("至少选择一项");
            return false;
        }
        if (interestDetailPassword.getText().length() == 0) {
            toast("金额不能为空");
            return false;
        }
        if (Integer.parseInt(interestDetailPassword.getText().toString()) <= 0) {
            toast("下注金额不能为0");
            return false;
        }
        return true;
    }

    @OnClick({R.id.interest_detail_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.interest_detail_back:
                if (isCheck()) {
                    interestSubmitNet();
                }
                break;
        }
    }
}
